package com.portalEsporteClubeBahia.app.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.portalEsporteClubeBahia.app.ApplicationException;
import com.portalEsporteClubeBahia.app.vo.ActionMatchVO;
import com.portalEsporteClubeBahia.app.vo.ChampionshipVO;
import com.portalEsporteClubeBahia.app.vo.MatchVO;
import com.portalEsporteClubeBahia.app.vo.StadiumVO;
import com.portalEsporteClubeBahia.app.vo.TeamVO;

public class MatchDAO extends DAOFactory {

	public MatchDAO() throws ApplicationException {
		super();
	}
	
	public List<MatchVO> findAllNotFinished() throws ApplicationException{
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		List<MatchVO> list = new ArrayList<MatchVO>();
		
		try{
			StringBuffer sql = new StringBuffer(" SELECT M.ID, M.championshipId, C.name, M.stadiumId, S.name, M.awayTeamId,  AT.name, AT.shortName, AT.urlImage, ");
			sql.append(" M.homeTeamId, ht.name, ht.shortName, ht.urlImage, M.date, M.dateUTC, M.homeGoals, M.awayGoals, M.finished, M.active ");
			sql.append(" FROM tbl_match M, tbl_championship C, tbl_stadium S, tbl_team AT, tbl_team HT ");
			sql.append(" WHERE ");
			sql.append(" C.id = M.championshipId ");
			sql.append(" AND S.id = M.stadiumId ");
			sql.append(" AND AT.ID = M.awayTeamId ");
			sql.append(" AND HT.ID = M.homeTeamId ");
			sql.append(" AND M.active = 1 ");
			sql.append(" AND M.finished = 0 ");
			sql.append(" ORDER BY M.date ");
			
			conn = this.getConnection();
			ps = conn.prepareStatement(sql.toString());
			rs = ps.executeQuery();
			
			while(rs.next()){
				MatchVO match = new MatchVO();
				match.setId(rs.getInt(1));
				ChampionshipVO championship = new ChampionshipVO();
				championship.setId(rs.getInt(2));
				championship.setName(rs.getString(3));
				match.setChampionship(championship);
				StadiumVO stadium = new StadiumVO();
				stadium.setId(rs.getInt(4));
				stadium.setName(rs.getString(5));
				match.setStadium(stadium);
				TeamVO awayTeam = new TeamVO();
				awayTeam.setId(rs.getInt(6));
				awayTeam.setName(rs.getString(7));
				awayTeam.setShortName(rs.getString(8));
				awayTeam.setUrlImage(rs.getString(9));
				match.setAwayTeam(awayTeam);
				TeamVO homeTeam = new TeamVO();
				homeTeam.setId(rs.getInt(10));
				homeTeam.setName(rs.getString(11));
				homeTeam.setShortName(rs.getString(12));
				homeTeam.setUrlImage(rs.getString(13));
				match.setHomeTeam(homeTeam);
				match.setDate(rs.getTimestamp(14));
				match.setDateUTC(rs.getTimestamp(15));
				match.setHomeGoals(rs.getShort(16));
				match.setAwayGoals(rs.getShort(17));
				match.setFinished(rs.getBoolean(18));
				match.setActive(rs.getBoolean(19));
				list.add(match);
			}
			
		}catch(Exception e){
			throw new ApplicationException(e);
		}finally{
			this.close(conn, ps, rs);
		}
		
		return list;
		
	}
	
	public List<MatchVO> findAll() throws ApplicationException{
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		List<MatchVO> list = new ArrayList<MatchVO>();
		
		try{
			StringBuffer sql = new StringBuffer(" SELECT M.ID, M.championshipId, C.name, M.stadiumId, S.name, M.awayTeamId,  AT.name, AT.shortName, AT.urlImage, ");
			sql.append(" M.homeTeamId, ht.name, ht.shortName, ht.urlImage, M.date, M.dateUTC, M.homeGoals, M.awayGoals, M.finished, M.active ");
			sql.append(" FROM tbl_match M, tbl_championship C, tbl_stadium S, tbl_team AT, tbl_team HT ");
			sql.append(" WHERE ");
			sql.append(" C.id = M.championshipId ");
			sql.append(" AND S.id = M.stadiumId ");
			sql.append(" AND AT.ID = M.awayTeamId ");
			sql.append(" AND HT.ID = M.homeTeamId ");
			sql.append(" AND M.active = 1 ");
			sql.append(" ORDER BY M.date ");
			
			conn = this.getConnection();
			ps = conn.prepareStatement(sql.toString());
			rs = ps.executeQuery();
			
			while(rs.next()){
				MatchVO match = new MatchVO();
				match.setId(rs.getInt(1));
				ChampionshipVO championship = new ChampionshipVO();
				championship.setId(rs.getInt(2));
				championship.setName(rs.getString(3));
				match.setChampionship(championship);
				StadiumVO stadium = new StadiumVO();
				stadium.setId(rs.getInt(4));
				stadium.setName(rs.getString(5));
				match.setStadium(stadium);
				TeamVO awayTeam = new TeamVO();
				awayTeam.setId(rs.getInt(6));
				awayTeam.setName(rs.getString(7));
				awayTeam.setShortName(rs.getString(8));
				awayTeam.setUrlImage(rs.getString(9));
				match.setAwayTeam(awayTeam);
				TeamVO homeTeam = new TeamVO();
				homeTeam.setId(rs.getInt(10));
				homeTeam.setName(rs.getString(11));
				homeTeam.setShortName(rs.getString(12));
				homeTeam.setUrlImage(rs.getString(13));
				match.setHomeTeam(homeTeam);
				match.setDate(rs.getTimestamp(14));
				match.setDateUTC(rs.getTimestamp(15));
				match.setHomeGoals(rs.getShort(16));
				match.setAwayGoals(rs.getShort(17));
				match.setFinished(rs.getBoolean(18));
				match.setActive(rs.getBoolean(19));
				list.add(match);
			}
			
		}catch(Exception e){
			throw new ApplicationException(e);
		}finally{
			this.close(conn, ps, rs);
		}
		
		return list;
		
	}
	
	public List<MatchVO> findByFilter(MatchVO matchFilter) throws ApplicationException{
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		List<MatchVO> list = new ArrayList<MatchVO>();
		
		try{
			StringBuffer sql = new StringBuffer(" SELECT M.ID, M.championshipId, C.name, M.stadiumId, S.name, M.awayTeamId,  AT.name, AT.shortName, AT.urlImage, ");
			sql.append(" M.homeTeamId, ht.name, ht.shortName, ht.urlImage, M.date, M.dateUTC, M.homeGoals, M.awayGoals, M.finished, M.active ");
			sql.append(" FROM tbl_match M, tbl_championship C, tbl_stadium S, tbl_team AT, tbl_team HT ");
			sql.append(" WHERE ");
			sql.append(" C.id = M.championshipId ");
			sql.append(" AND S.id = M.stadiumId ");
			sql.append(" AND AT.ID = M.awayTeamId ");
			sql.append(" AND HT.ID = M.homeTeamId ");
			if(matchFilter.getDateString()!=null && !matchFilter.getDateString().equalsIgnoreCase("")){
				sql.append(" AND (M.DATE >= '"+matchFilter.getDateStringFormat()+" 00:00:00' AND M.DATE <= '"+matchFilter.getDateStringFormat()+" 23:59:59') ");
			}
			if(matchFilter.getDateUTCString()!=null && !matchFilter.getDateUTCString().equalsIgnoreCase("")){
				sql.append(" AND (M.DATEUTC >= '"+matchFilter.getDateUTCStringFormat()+" 00:00:00' AND M.DATEUTC <= '"+matchFilter.getDateUTCStringFormat()+" 23:59:59') ");
			}
			sql.append(" AND (? = 0 OR M.awayTeamId = ?) ");
			sql.append(" AND (? = 0 OR M.homeTeamId = ?) ");
			sql.append(" AND (? = 0 OR M.stadiumId = ?) ");
			sql.append(" AND (? = 0 OR M.championshipId = ?) ");
			sql.append(" AND M.active = ? ");
			sql.append(" AND M.finished = ? ");
			sql.append(" ORDER BY M.date DESC ");
			
			conn = this.getConnection();
			ps = conn.prepareStatement(sql.toString());
			if(matchFilter.getAwayTeam().getId()==null){
				ps.setInt(1, 0);
				ps.setInt(2, 0);
			}else{
				ps.setInt(1, matchFilter.getAwayTeam().getId());
				ps.setInt(2, matchFilter.getAwayTeam().getId());
			}
			
			if(matchFilter.getAwayTeam().getId()==null){
				ps.setInt(3, 0);
				ps.setInt(4, 0);
			}else{
				ps.setInt(3, matchFilter.getHomeTeam().getId());
				ps.setInt(4, matchFilter.getHomeTeam().getId());
			}
			
			if(matchFilter.getStadium().getId()==null){
				ps.setInt(5, 0);
				ps.setInt(6, 0);
			}else{
				ps.setInt(5, matchFilter.getStadium().getId());
				ps.setInt(6, matchFilter.getStadium().getId());
			}
			
			if(matchFilter.getChampionship().getId()==null){
				ps.setInt(7, 0);
				ps.setInt(8, 0);
			}else{
				ps.setInt(7, matchFilter.getChampionship().getId());
				ps.setInt(8, matchFilter.getChampionship().getId());
			}
			
			ps.setBoolean(9, matchFilter.getActive());
			ps.setBoolean(10, matchFilter.getFinished());
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				MatchVO match = new MatchVO();
				match.setId(rs.getInt(1));
				ChampionshipVO championship = new ChampionshipVO();
				championship.setId(rs.getInt(2));
				championship.setName(rs.getString(3));
				match.setChampionship(championship);
				StadiumVO stadium = new StadiumVO();
				stadium.setId(rs.getInt(4));
				stadium.setName(rs.getString(5));
				match.setStadium(stadium);
				TeamVO awayTeam = new TeamVO();
				awayTeam.setId(rs.getInt(6));
				awayTeam.setName(rs.getString(7));
				awayTeam.setShortName(rs.getString(8));
				awayTeam.setUrlImage(rs.getString(9));
				match.setAwayTeam(awayTeam);
				TeamVO homeTeam = new TeamVO();
				homeTeam.setId(rs.getInt(10));
				homeTeam.setName(rs.getString(11));
				homeTeam.setShortName(rs.getString(12));
				homeTeam.setUrlImage(rs.getString(13));
				match.setHomeTeam(homeTeam);
				match.setDate(rs.getTimestamp(14));
				match.setDateUTC(rs.getTimestamp(15));
				match.setHomeGoals(rs.getShort(16));
				match.setAwayGoals(rs.getShort(17));
				match.setFinished(rs.getBoolean(18));
				match.setActive(rs.getBoolean(19));
				list.add(match);
			}
			
		}catch(Exception e){
			throw new ApplicationException(e);
		}finally{
			this.close(conn, ps, rs);
		}
		
		return list;
		
	}
	
	public List<MatchVO> findByTeam(MatchVO matchFilter) throws ApplicationException{
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		List<MatchVO> list = new ArrayList<MatchVO>();
		
		try{
			StringBuffer sql = new StringBuffer(" SELECT M.ID, M.championshipId, C.name, M.stadiumId, S.name, M.awayTeamId,  AT.name, AT.shortName, AT.urlImage, ");
			sql.append(" M.homeTeamId, ht.name, ht.shortName, ht.urlImage, M.date, M.dateUTC, M.homeGoals, M.awayGoals, M.finished, M.active ");
			sql.append(" FROM tbl_match M, tbl_championship C, tbl_stadium S, tbl_team AT, tbl_team HT ");
			sql.append(" WHERE ");
			sql.append(" C.id = M.championshipId ");
			sql.append(" AND S.id = M.stadiumId ");
			sql.append(" AND AT.ID = M.awayTeamId ");
			sql.append(" AND HT.ID = M.homeTeamId ");
			sql.append(" AND M.active = 1 ");
			sql.append(" AND ((? = 0 OR M.awayTeamId = ?) OR (? = 0 OR M.homeTeamId = ?)) ");
			sql.append(" ORDER BY M.date ");
			
			conn = this.getConnection();
			ps = conn.prepareStatement(sql.toString());
			if(matchFilter.getAwayTeam().getId()==null){
				ps.setInt(1, 0);
				ps.setInt(2, 0);
			}else{
				ps.setInt(1, matchFilter.getAwayTeam().getId());
				ps.setInt(2, matchFilter.getAwayTeam().getId());
			}
			
			if(matchFilter.getHomeTeam().getId()==null){
				ps.setInt(3, 0);
				ps.setInt(4, 0);
			}else{
				ps.setInt(3, matchFilter.getHomeTeam().getId());
				ps.setInt(4, matchFilter.getHomeTeam().getId());
			}
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				MatchVO match = new MatchVO();
				match.setId(rs.getInt(1));
				ChampionshipVO championship = new ChampionshipVO();
				championship.setId(rs.getInt(2));
				championship.setName(rs.getString(3));
				match.setChampionship(championship);
				StadiumVO stadium = new StadiumVO();
				stadium.setId(rs.getInt(4));
				stadium.setName(rs.getString(5));
				match.setStadium(stadium);
				TeamVO awayTeam = new TeamVO();
				awayTeam.setId(rs.getInt(6));
				awayTeam.setName(rs.getString(7));
				awayTeam.setShortName(rs.getString(8));
				awayTeam.setUrlImage(rs.getString(9));
				match.setAwayTeam(awayTeam);
				TeamVO homeTeam = new TeamVO();
				homeTeam.setId(rs.getInt(10));
				homeTeam.setName(rs.getString(11));
				homeTeam.setShortName(rs.getString(12));
				homeTeam.setUrlImage(rs.getString(13));
				match.setHomeTeam(homeTeam);
				match.setDate(rs.getTimestamp(14));
				match.setDateUTC(rs.getTimestamp(15));
				match.setHomeGoals(rs.getShort(16));
				match.setAwayGoals(rs.getShort(17));
				match.setFinished(rs.getBoolean(18));
				match.setActive(rs.getBoolean(19));
				list.add(match);
			}
			
		}catch(Exception e){
			throw new ApplicationException(e);
		}finally{
			this.close(conn, ps, rs);
		}
		
		return list;
		
	}
	
	public List<ActionMatchVO> findActionsByMatch(MatchVO matchFilter) throws ApplicationException{
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		List<ActionMatchVO> list = new ArrayList<ActionMatchVO>();
		
		try{
			StringBuffer sql = new StringBuffer(" SELECT * FROM TBL_ACTION WHERE MATCHID = ? ORDER BY MINUTES DESC, ID DESC");
			
			conn = this.getConnection();
			ps = conn.prepareStatement(sql.toString());
			ps.setInt(1, matchFilter.getId());
			rs = ps.executeQuery();
			
			while(rs.next()){
				ActionMatchVO action = new ActionMatchVO();
				action.setId(rs.getLong("ID"));
				action.setDescription(rs.getString("DESCRIPTION"));
				action.setMinutes(rs.getInt("MINUTES"));
				list.add(action);
			}
			
		}catch(Exception e){
			throw new ApplicationException(e);
		}finally{
			this.close(conn, ps, rs);
		}
		
		return list;
		
	}
	
	public void insert(ActionMatchVO action) throws ApplicationException{
		
		Connection conn = null;
		PreparedStatement ps = null;
		
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO TBL_ACTION (MATCHID, DESCRIPTION, MINUTES) VALUES (?,?,?)");
		
		try{
			
			conn = this.getConnection();
			ps = conn.prepareStatement(sql.toString());
			ps.setInt(1, action.getMatch().getId());
			ps.setString(2, action.getDescription());
			ps.setInt(3, action.getMinutes());
			ps.executeUpdate();
			
		}catch(Exception e){
			throw new ApplicationException(e);
		}finally{
			this.close(conn, ps, null);
		}
		
	}
	
	public void update(ActionMatchVO action) throws ApplicationException{
		
		Connection conn = null;
		PreparedStatement ps = null;
		
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE TBL_ACTION SET DESCRIPTION = ?, MINUTES = ? WHERE ID = ?");
		
		try{
			
			conn = this.getConnection();
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, action.getDescription());
			ps.setInt(2, action.getMinutes());
			ps.setLong(3, action.getId());
			ps.executeUpdate();
			
		}catch(Exception e){
			throw new ApplicationException(e);
		}finally{
			this.close(conn, ps, null);
		}
		
	}
	
	public void delete(ActionMatchVO action) throws ApplicationException{
		
		Connection conn = null;
		PreparedStatement ps = null;
		
		StringBuffer sql = new StringBuffer();
		sql.append("delete from TBL_ACTION WHERE ID = ?");
		
		try{
			
			conn = this.getConnection();
			ps = conn.prepareStatement(sql.toString());
			ps.setLong(1, action.getId());
			ps.executeUpdate();
			
		}catch(Exception e){
			throw new ApplicationException(e);
		}finally{
			this.close(conn, ps, null);
		}
		
	}
	
	public void insert(MatchVO match) throws ApplicationException{
		
		Connection conn = null;
		PreparedStatement ps = null;
		
		StringBuffer sql = new StringBuffer();
		sql.append(" INSERT INTO TBL_MATCH ");
		sql.append(" (CHAMPIONSHIPID, STADIUMID, AWAYTEAMID, HOMETEAMID, DATE, DATEUTC, HOMEGOALS, AWAYGOALS, FINISHED, ACTIVE) ");
		sql.append(" VALUES ");
		sql.append(" (?,?,?,?,?,?,NULL,NULL,?,?) ");
		
		try{
			
			conn = this.getConnection();
			ps = conn.prepareStatement(sql.toString());
			ps.setInt(1, match.getChampionship().getId());
			ps.setInt(2, match.getStadium().getId());
			ps.setInt(3, match.getAwayTeam().getId());
			ps.setInt(4, match.getHomeTeam().getId());
			Timestamp timestamp = new Timestamp(match.getDate().getTime());
			ps.setTimestamp(5, timestamp);
			timestamp = new Timestamp(match.getDate().getTime());
			ps.setTimestamp(6, timestamp);
			ps.setBoolean(7, match.getFinished());
			ps.setBoolean(8, match.getActive());
			ps.executeUpdate();
			
		}catch(Exception e){
			throw new ApplicationException(e);
		}finally{
			this.close(conn, ps, null);
		}
		
	}
	
	public void update(MatchVO match) throws ApplicationException{
		
		Connection conn = null;
		PreparedStatement ps = null;
		
		StringBuffer sql = new StringBuffer();
		sql.append(" UPDATE TBL_MATCH ");
		sql.append(" SET CHAMPIONSHIPID=?, STADIUMID=?, AWAYTEAMID=?, HOMETEAMID=?, DATE=?, DATEUTC=?, HOMEGOALS=?, AWAYGOALS=?, FINISHED=?, ACTIVE=? ");
		sql.append(" WHERE ");
		sql.append(" ID = ? ");
		
		try{
			
			conn = this.getConnection();
			ps = conn.prepareStatement(sql.toString());
			ps.setInt(1, match.getChampionship().getId());
			ps.setInt(2, match.getStadium().getId());
			ps.setInt(3, match.getAwayTeam().getId());
			ps.setInt(4, match.getHomeTeam().getId());
			Timestamp timestamp = new Timestamp(match.getDate().getTime());
			ps.setTimestamp(5, timestamp);
			timestamp = new Timestamp(match.getDate().getTime());
			ps.setTimestamp(6, timestamp);
			ps.setShort(7, match.getHomeGoals());
			ps.setShort(8, match.getAwayGoals());
			ps.setBoolean(9, match.getFinished());
			ps.setBoolean(10, match.getActive());
			ps.setInt(11, match.getId());
			ps.executeUpdate();
			
		}catch(Exception e){
			throw new ApplicationException(e);
		}finally{
			this.close(conn, ps, null);
		}
		
	}
	
	public void delete(MatchVO match) throws ApplicationException{
		
		Connection conn = null;
		PreparedStatement ps = null;
		
		StringBuffer sql = new StringBuffer();
		sql.append(" DELETE FROM TBL_MATCH WHERE ID = ? ");
		
		try{
			
			conn = this.getConnection();
			ps = conn.prepareStatement(sql.toString());
			ps.setLong(1, match.getId());
			ps.executeUpdate();
			
		}catch(Exception e){
			throw new ApplicationException(e);
		}finally{
			this.close(conn, ps, null);
		}
		
	}
	
	public MatchVO findByPrimaryKey(MatchVO matchFilter) throws ApplicationException{
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		MatchVO match = new MatchVO();
		
		try{
			StringBuffer sql = new StringBuffer(" SELECT M.ID, M.championshipId, C.name, M.stadiumId, S.name, M.awayTeamId,  AT.name, AT.shortName, AT.urlImage, ");
			sql.append(" M.homeTeamId, ht.name, ht.shortName, ht.urlImage, M.date, M.dateUTC, M.homeGoals, M.awayGoals, M.finished, M.active ");
			sql.append(" FROM tbl_match M, tbl_championship C, tbl_stadium S, tbl_team AT, tbl_team HT ");
			sql.append(" WHERE ");
			sql.append(" C.id = M.championshipId ");
			sql.append(" AND S.id = M.stadiumId ");
			sql.append(" AND AT.ID = M.awayTeamId ");
			sql.append(" AND HT.ID = M.homeTeamId ");
			sql.append(" AND M.ID = ? ");
			
			conn = this.getConnection();
			ps = conn.prepareStatement(sql.toString());
			ps.setInt(1, matchFilter.getId());
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				
				match.setId(rs.getInt(1));
				ChampionshipVO championship = new ChampionshipVO();
				championship.setId(rs.getInt(2));
				championship.setName(rs.getString(3));
				match.setChampionship(championship);
				StadiumVO stadium = new StadiumVO();
				stadium.setId(rs.getInt(4));
				stadium.setName(rs.getString(5));
				match.setStadium(stadium);
				TeamVO awayTeam = new TeamVO();
				awayTeam.setId(rs.getInt(6));
				awayTeam.setName(rs.getString(7));
				awayTeam.setShortName(rs.getString(8));
				awayTeam.setUrlImage(rs.getString(9));
				match.setAwayTeam(awayTeam);
				TeamVO homeTeam = new TeamVO();
				homeTeam.setId(rs.getInt(10));
				homeTeam.setName(rs.getString(11));
				homeTeam.setShortName(rs.getString(12));
				homeTeam.setUrlImage(rs.getString(13));
				match.setHomeTeam(homeTeam);
				match.setDate(rs.getTimestamp(14));
				match.setDateUTC(rs.getTimestamp(15));
				match.setHomeGoals(rs.getShort(16));
				match.setAwayGoals(rs.getShort(17));
				match.setFinished(rs.getBoolean(18));
				match.setActive(rs.getBoolean(19));
			}
			
		}catch(Exception e){
			throw new ApplicationException(e);
		}finally{
			this.close(conn, ps, rs);
		}
		
		return match;
		
	}
	
	public List<ActionMatchVO> findByFilter(ActionMatchVO action) throws ApplicationException{
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		List<ActionMatchVO> list = new ArrayList<ActionMatchVO>();
		
		try{
			
			StringBuffer sql = new StringBuffer();
			sql.append(" SELECT ID, MATCHID, MINUTES, DESCRIPTION FROM TBL_ACTION ");
			sql.append(" WHERE 1=1 ");
			sql.append(" AND (? = 0 OR MATCHID = ?) ");
			sql.append(" ORDER BY MINUTES DESC ");
			
			conn = this.getConnection();
			ps = conn.prepareStatement(sql.toString());
			if(action.getMatch().getId()!=null){
				ps.setInt(1, action.getMatch().getId());
				ps.setInt(2, action.getMatch().getId());
			}else{
				ps.setInt(1, 0);
				ps.setInt(2, 0);
			}
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				ActionMatchVO vo = new ActionMatchVO();
				vo.setId(rs.getLong(1));
				vo.getMatch().setId(rs.getInt(2));
				vo.setMinutes(rs.getInt(3));
				vo.setDescription(rs.getString(4));
				list.add(vo);
			}
			
		}catch(Exception e){
			throw new ApplicationException(e);
		}finally{
			this.close(conn, ps, rs);
		}
		
		return list;
		
	}

}
